package com.sqfw.project.knowledge.controller;

import java.io.IOException;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.sqfw.project.knowledge.domain.KlContentFileInfo;
import com.sqfw.project.knowledge.service.KlContentFileInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.sqfw.framework.aspectj.lang.annotation.Log;
import com.sqfw.framework.aspectj.lang.enums.BusinessType;
import com.sqfw.framework.web.controller.BaseController;
import com.sqfw.framework.web.domain.AjaxResult;
import com.sqfw.common.utils.poi.ExcelUtil;
import com.sqfw.framework.web.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 知识内容附件信息Controller
 * 
 * @author tangxt
 * @date 2023-06-09
 */
@Api(tags = "知识库上传文件")
@RestController
@RequestMapping("/klContentFileInfo")
public class KlContentFileInfoController extends BaseController
{
    @Autowired
    private KlContentFileInfoService klContentFileInfoService;

    /**
     * 查询知识内容附件信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:info:list')")
    @GetMapping("/list")
    public TableDataInfo list(KlContentFileInfo klContentFileInfo)
    {
        startPage();
        List<KlContentFileInfo> list = klContentFileInfoService.selectKlContentFileInfoList(klContentFileInfo);
        return getDataTable(list);
    }

    /**
     * 导出知识内容附件信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:info:export')")
    @Log(title = "知识内容附件信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, KlContentFileInfo klContentFileInfo)
    {
        List<KlContentFileInfo> list = klContentFileInfoService.selectKlContentFileInfoList(klContentFileInfo);
        ExcelUtil<KlContentFileInfo> util = new ExcelUtil<KlContentFileInfo>(KlContentFileInfo.class);
        util.exportExcel(response, list, "知识内容附件信息数据");
    }

    /**
     * 获取知识内容附件信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:info:query')")
    @GetMapping(value = "/{klId}")
    public AjaxResult getInfo(@PathVariable("klId") String klId)
    {
        return success(klContentFileInfoService.selectKlContentFileInfoByKlId(klId));
    }

    /**
     * 新增知识内容附件信息
     */
    @PreAuthorize("@ss.hasPermi('system:info:add')")
    @Log(title = "知识内容附件信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody KlContentFileInfo klContentFileInfo)
    {
        return toAjax(klContentFileInfoService.insertKlContentFileInfo(klContentFileInfo));
    }

    /**
     * 修改知识内容附件信息
     */
    @PreAuthorize("@ss.hasPermi('system:info:edit')")
    @Log(title = "知识内容附件信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody KlContentFileInfo klContentFileInfo)
    {
        return toAjax(klContentFileInfoService.updateKlContentFileInfo(klContentFileInfo));
    }

    /**
     * 删除知识内容附件信息
     */
    @PreAuthorize("@ss.hasPermi('system:info:remove')")
    @Log(title = "知识内容附件信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{klIds}")
    public AjaxResult remove(@PathVariable String[] klIds)
    {
        return toAjax(klContentFileInfoService.deleteKlContentFileInfoByKlIds(klIds));
    }


    /**
     * 删除知识内容附件信息--逻辑删除
     */
//    @PreAuthorize("@ss.hasPermi('system:info:remove')")
    @Log(title = "删除知识附件", businessType = BusinessType.DELETE)
    @PostMapping("deleteFile")
    public AjaxResult deleteFile(@RequestBody KlContentFileInfo klContentFileInfo) {
        if (StringUtils.isBlank(klContentFileInfo.getKlId())) {
            return AjaxResult.error("文件id必传");
        }
        return toAjax(klContentFileInfoService.deleteKlContentFileInfoByKlId(klContentFileInfo.getKlId()));
    }


    /**
     * 文件上传，成功返回文件id
     * @param file
     * @return
     * @throws IOException
     */
    @ApiOperation(value = "文件上传", consumes = "application/text",
            httpMethod = "POST", response = KlContentFileInfo.class)
    @PostMapping("/uploadFile")
    public AjaxResult uploadFile(MultipartFile file) throws IOException {
        if (file.isEmpty()) {
            return AjaxResult.error("上传失败，请选择文件!");
        }
        return klContentFileInfoService.uploadFile(file);
    }


}
